<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    
</body>
<script>
    // let p1 = new Promise((resolve, reject)=>{
    //     reject("失败");
    // }).catch(
    //     (e)=>console.log(e)
    // )

    // let p1 = new Promise((resolve, reject)=>{
    //     resolve("成功");
    // }).then(
    //     ()=>{return new Promise();}
    // )
    // .catch(
    //     (e)=>console.log(e)
    // )


    //非重入期约方法
    // let p = Promise.resolve("resolve");
    // p.then((result) => {
    //     console.log(1+" "+result)
    // }).catch((err) => {
    //     console.log(2+"err")
    // });
    // console.log(3+" then has return!");
    //先输出同步代码结果

    //期约连锁
    /*let p1 = new Promise((resolve, reject)=>{
        console.log('p1 executor');
        setTimeout(resolve, 1000);
    });
    p1.then(()=>new Promise((resolve, reject)=>{
        console.log('p2 executor');
        setTimeout(resolve, 1000);
    })).then(()=>new Promise((resolve,reject)=>{
        console.log('p3 executor');
        setTimeout(resolve, 1000);
    })).then(()=>new Promise((resolve,reject)=>{
        console.log('p4 executor');
        setTimeout(resolve, 1000);
    }));
    */
    /*
    function delayedResolve(str) {
            return new Promise((resolve, reject) => {
                console.log(str);
                setTimeout(resolve, 1000);
            });
        }

        delayedResolve('p1 executor')
            .then(() => delayedResolve('p2 executor'))
            .then(() => delayedResolve('p3 executor'))
            .then(() => delayedResolve('p4 executor'))
    */

    //期约合成
    /*let p = Promise.all([
        Promise.resolve(),
        new Promise((resolve, reject)=>setTimeout(resolve, 1000))
    ]);
    setTimeout(console.log, 0, p);
    p.then(()=>setTimeout(console.log, 0, 'all() resolved!'))
    
    永远等待
    let p = Promise.all([
        new Promise(()=>{}),
        new Promise((resolve, reject)=>setTimeout(resolve, 1000))
    ]);
    setTimeout(console.log, 0, p);
    p.then(()=>setTimeout(console.log, 0, 'all() resolved!'))  
    
    一次拒绝，最终期约拒绝
        let p2 = Promise.all([
        Promise.resolve(3),
        Promise.resolve(4),
        Promise.reject(5)
    ]);
    p2.then((value)=> setTimeout(console.log, 1000, value));
    */
   
   //Promise.race()
   /*let p1 = Promise.race([
        Promise.resolve(),
        Promise.resolve()
   ]);
   //解决先发生，超时后的拒绝被忽略
    let p1 = Promise.race([
        Promise.resolve(3),
        new Promise((resolve, reject)=>setTimeout(reject, 1000))
    ]);
    p1.then(()=>setTimeout(console.log, 0, p1));
    */
    //拒绝先发生，超时后的解决被忽略
    let p2 = Promise.race([
        Promise.reject(4),
        new Promise((resolve, reject)=>setTimeout(resolve, 1000))
    ]);
    setTimeout(console.log, 0, p2);
</script>
</html>